//
//  ZYStarchongView.m
//  BestLearning
//
//  Created by Ethan on 2017/9/14.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ZYStarchongView.h"
#define TextFont [UIFont systemFontOfSize:16]


@interface ZYStarchongView ()

@property(nonatomic, strong) UILabel *totalMoneyLabel;  //课程金额



@property(nonatomic, strong) UILabel *withholdLabel_2;  //星币代扣金额



@property(nonatomic, strong) UIImageView *WxPayIconImageView;   //微信图标

@property(nonatomic, strong) UILabel *WxPayLabel;   //微信标题

@property(nonatomic, strong) UIButton *WxPaySelectButton;   //微信选中图标


@property(nonatomic, strong) UIImageView *zfbPayIconImageView;   //支付宝图标

@property(nonatomic, strong) UILabel *zfbPayLabel;   //支付宝

@property(nonatomic, strong) UIButton *zfbPaySelectButton;   //支付宝选中图标

@property(nonatomic, strong) UIButton *payButton;   //付款button
@property (nonatomic,assign) NSInteger selectTag;



@end

@implementation ZYStarchongView
-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 8.0f;
        
        
        _totalMoneyLabel = [[UILabel alloc]init];
        _totalMoneyLabel.font = TextFont;
        _totalMoneyLabel.text = @"课程金额: ¥0.2";
        [self addSubview:_totalMoneyLabel];
        
        
        _withholdLabel_1 = [[UILabel alloc]init];
        _withholdLabel_1.font = TextFont;
        _withholdLabel_1.text = @"星币代扣: 0";
        [self addSubview:_withholdLabel_1];
        
        
        _withholdLabel_2 = [[UILabel alloc]init];
        _withholdLabel_2.font = TextFont;
        _withholdLabel_2.text = @"星币代扣: - ¥0.0";
        _withholdLabel_2.textColor = [UIColor redColor];
        _withholdLabel_2.textAlignment = NSTextAlignmentRight;
        [self addSubview:_withholdLabel_2];
        
        _realityMoneyLabel = [[UILabel alloc]init];
        _realityMoneyLabel.font = TextFont;
        _realityMoneyLabel.text = @"实际金额:  ￥1.0";
        [self addSubview:_realityMoneyLabel];
        
        //微信
        _WxPayIconImageView = [[UIImageView alloc]init];
        _WxPayIconImageView.image = [UIImage imageNamed:@"recharge_icon_wechat"];
        [self addSubview:_WxPayIconImageView];
        
        
        _WxPayLabel = [[UILabel alloc]init];
        _WxPayLabel.font = TextFont;
        _WxPayLabel.text = @"微信支付";
        [self addSubview:_WxPayLabel];
        
        _WxPaySelectButton = [[UIButton alloc]init];
        [_WxPaySelectButton setBackgroundImage:[UIImage imageNamed:@"recharge_icon_circle_normal"] forState:UIControlStateNormal];//
        self.WxPaySelectButton.tag = 201;//
        [self addSubview:_WxPaySelectButton];
        
        
        //支付宝
        _zfbPayIconImageView = [[UIImageView alloc]init];
        _zfbPayIconImageView.image = [UIImage imageNamed:@"recharge_icon_zfb"];
        [self addSubview:_zfbPayIconImageView];
        
        _zfbPayLabel = [[UILabel alloc]init];
        _zfbPayLabel.font = TextFont;
        _zfbPayLabel.text = @"支付宝支付";
        [self addSubview:_zfbPayLabel];
        
        _zfbPaySelectButton = [[UIButton alloc]init];
        [_zfbPaySelectButton setBackgroundImage:[UIImage imageNamed:@"recharge_icon_circle_normal"] forState:UIControlStateNormal];
        self.zfbPaySelectButton.tag = 202;
        [self addSubview:_zfbPaySelectButton];
        
        
        _payButton = [[UIButton alloc]init];
        [_payButton setTitle:@"付 款" forState:UIControlStateNormal];
        [_payButton setBackgroundColor:ColorRGB(139, 198, 253)];
        _payButton.layer.cornerRadius = 5;
        _payButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_payButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:_payButton];
        
        [_payButton addTarget:self action:@selector(payButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_zfbPaySelectButton addTarget:self action:@selector(wxPaySelectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [_WxPaySelectButton addTarget:self action:@selector(wxPaySelectButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(payViewTapped)];
        
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

-(void)layoutSubviews{

    [super layoutSubviews];
    
    CGSize makeSize = CGSizeMake(self.width - 40, 15);
    
    [_totalMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(20);
        make.top.equalTo(self.mas_top).offset(15);
        make.size.mas_equalTo(makeSize);
    }];
    
    [_withholdLabel_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_totalMoneyLabel.mas_left);
        make.top.equalTo(_totalMoneyLabel.mas_bottom).offset(20);
        make.size.mas_equalTo(makeSize);
    }];
    
    

    
    //微信
    [_WxPayIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_withholdLabel_1.mas_left);
        make.top.equalTo(_withholdLabel_1.mas_bottom).offset(80);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [_WxPayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_WxPayIconImageView.mas_right).offset(20);
        make.centerY.equalTo(_WxPayIconImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 15));
    }];
    
    [_WxPaySelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-20);
        make.centerY.equalTo(_WxPayIconImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    //支付宝
    
    [_zfbPayIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_withholdLabel_1.mas_left);
        make.top.equalTo(_WxPayIconImageView.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [_zfbPayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_zfbPayIconImageView.mas_right).offset(20);
        make.centerY.equalTo(_zfbPayIconImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(100, 15));
    }];
    
    [_zfbPaySelectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-20);
        make.centerY.equalTo(_zfbPayIconImageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    
    
    
    [_payButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-20);
        make.left.equalTo(_zfbPayIconImageView);
        make.bottom.equalTo(self.mas_bottom).offset(-15);
        // make.size.mas_equalTo(CGSizeMake(100, 35));
    }];
}


-(void)setPrice:(double )price{
    
    _price = price;
    
    self.totalMoneyLabel.text = [NSString stringWithFormat:@"充值金额: ￥%.2f",price];
    
    
}





-(void)payViewTapped{
    
    if (self.payViewTappedBlock) {
        self.payViewTappedBlock();
    }
}

-(void)payButtonClicked:(UIButton *)button{
    
    if (self.selectTag ==0) {
        [MBProgressHUD showError:@"请选择支付类型"];
    }else{
        if(self.payButtonClickedBlock){
            
            self.payButtonClickedBlock(button,self.selectTag);
        }
        
    }
    
}

-(void)wxPaySelectButtonClicked:(UIButton *)button{
    
    NSInteger tag = button.tag;
    
    if (201 ==tag) {//微信
        [_WxPaySelectButton setBackgroundImage:[UIImage imageNamed:@"recharge_icon_circle_press"] forState:UIControlStateNormal];
        [self.zfbPaySelectButton setBackgroundImage:[UIImage imageNamed:@"recharge_icon_circle_normal"] forState:UIControlStateNormal];
        self.selectTag = tag;
        if (self.wxSelectButtonClickedBlock) {
            self.wxSelectButtonClickedBlock(button,tag);
        }
    }else if (202 == tag){//支付宝
        [self.zfbPaySelectButton setBackgroundImage:[UIImage imageNamed:@"recharge_icon_circle_press"] forState:UIControlStateNormal];
        [_WxPaySelectButton setBackgroundImage:[UIImage imageNamed:@"recharge_icon_circle_normal"] forState:UIControlStateNormal];
        self.selectTag = tag;
        if (self.zfbSelectButtonClickedBlock) {
            self.zfbSelectButtonClickedBlock(button,tag);
        }
        
    }
    
    
    
    
}


@end
